# -*- coding: utf-8 -*-
# @Time    : 2020/3/15 下午5:58
# @Author  : lilong

"""
使用python 自带的attrgetter进行排序
"""

from operator import itemgetter, attrgetter


class Person:
    """人类"""

    def __init__(self, name, age, weight, height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

    def showMysef(self):
        print("%s 年龄：%d岁，体重：%0.2fkg，身高：%0.2fm" % (self.name, self.age, self.weight, self.height))


def testPersonListInPython():
    """用Python的方式对Person进行排序"""

    personList = [
        Person("Tony", 2, 54.5, 0.82),
        Person("Jack", 31, 74.5, 1.80),
        Person("Nick", 54, 44.5, 1.59),
        Person("Eric", 23, 62.0, 1.78),
        Person("Helen", 16, 45.7, 1.60)
    ]

    # 使用operator模块根据年龄、身高进行排序
    sortedPerons = sorted(personList, key=attrgetter('age'))
    sortedPerons1 = sorted(personList, key=attrgetter('height'))

    print("根据年龄进行排序后的结果：")
    for person in sortedPerons:
        person.showMysef()
    print()

    print("根据身高进行排序后的结果：")
    for person in sortedPerons1:
        person.showMysef()


testPersonListInPython()
